package com.libre.workflow.controller;

import com.libre.common.base.BaseController;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.Result;
import com.libre.workflow.service.MyResourceService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.ui.modeler.model.ModelRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 资源管理
 * @date 2020/9/11 18:01
 **/
@Slf4j
@RestController
@RequestMapping("/workflow/resource")
public class MyResourceController extends BaseController {

    @Autowired
    private MyResourceService resourceService;
//
//    /**
//     * 功能描述: 查看流程图片/XML【查看图片：只是一个图片，不会显示执行到那个节点（流程执行完毕 ！！！）】
//     *          访问示例： http://localhost:8888/model/resource?procDefId=askForLeave:1:8b0c7d46-f30c-11ea-a46e-405bd8b55521&proInsId=c6dc39f7-f30c-11ea-a46e-405bd8b55521&resType=image
//     * @author: 庞留杰
//     * @param processDefinitionId:流程定义ID
//     * @param processInstanceId:流程实例ID
//     * @param resourceType:资源类型(xml|image)
//     * @return:
//     * @exception
//     * @date: 2020/9/11 19:04
//     */
//    @GetMapping("/resourceRead")
//    public void resourceRead(String processDefinitionId, String processInstanceId, String resourceType, HttpServletResponse response) throws Exception {
//        service.resourceRead(processDefinitionId, processInstanceId, resourceType, response);
//    }
//
//    /**
//     * 功能描述: 生成当前流程图表【会显示执行到那个节点，但是一旦流程结束，图片便会失效，只可以用作执行过程中的流程图展示（流程正在执行中...）】
//     * @author: 庞留杰
//     * @param processInstanceId:流程实例Id
//     * @return:
//     * @exception
//     * @date: 2020/9/11 19:21
//     */
//    @GetMapping(value = "processDiagram")
//    public void getProcessDiagram(String processInstanceId, HttpServletResponse response) throws Exception {
//        service.getProcessDiagram(processInstanceId, response);
//    }
//
//

    /**
     * 功能描述: 获取流程图（包含 正在进行中+已结束 的流程图）
     *
     * @param processInstanceId 流程实例ID
     * @param executionId       执行实例ID（只用并行、多实例中才存在）
     * @throws
     * @author: 庞留杰
     * @return:
     * @date: 2021/4/16 13:43
     */
    @GetMapping(value = "flowDiagram")
    public void flowDiagram(String processInstanceId, @RequestParam(required = false) String executionId, HttpServletResponse response) {
        resourceService.flowDiagram(processInstanceId, executionId, response);
    }

    /**
     * 功能描述: 下载xml
     * 摘抄源码：org.flowable.ui.modeler.rest.app -> /rest/models/{processModelId}/bpmn20
     *
     * @param processDefinitionId:流程定义ID
     * @throws
     * @author: 庞留杰
     * @return:
     * @date: 2020/9/11 17:57
     */
    @GetMapping("/downloadXml/{processDefinitionId}")
    public void downloadXml(@PathVariable("processDefinitionId") String processDefinitionId, HttpServletResponse response) {
        resourceService.downloadXml(processDefinitionId, response);
    }
//
//
//    @PostMapping(value = "/files/zip", headers = "content-type=multipart/form-data")
//    @ApiOperation(value = "部署压缩包形式的模板(.zip .bar)，主子流程定义部署", notes = "可用于一次性部署多个资源文件（.bpmn .drl .form等）")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name", value = "主模板名称（模板ID）", required = true, dataType = "String"),
//            @ApiImplicitParam(name = "category", value = "模板类别", required = false, dataType = "String"),
//            @ApiImplicitParam(name = "tenantId", value = "系统标识", required = false, dataType = "String"),
//            @ApiImplicitParam(name = "file", value = "模板文件", required = true, dataType = "__file")
//    })
//    public Result deployByZip(String name, String category, String tenantId, MultipartFile file) {
//        Deployment deployment = service.deployByZip(name, category, tenantId, file);
//        return new ResultUtil<>().setData(deployment);
//    }
//

    /**
     * 功能描述: 利用此方式,可以将文件导入到"act_de_model"中
     * 摘抄源码: org.flowable.ui.modeler.rest -> POST "app/rest/import-case-model"
     *
     * @param file
     * @throws
     * @author: 庞留杰
     * @return: Result
     * @date: 2020/9/21 10:48
     */
    @PostMapping(value = "/importModel", produces = "application/json")
    public Result importModel(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        ModelRepresentation modelRepresentation = resourceService.importModel(file, request);
        return new ResultUtil<>().setData(modelRepresentation);
    }

}
